package com.common.base.tools.utils.language;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;

import androidx.activity.ComponentActivity;

import com.common.base.tools.utils.ApplicationUtils;
import com.common.base.tools.utils.Logs;

import java.util.Locale;

/**
 * 三疯
 * *************************
 * <p>
 * *************************
 */
public class MultiLanguageUtil {

    private static final String TAG = "MultiLanguageUtil";
    private static MultiLanguageUtil instance;
    public static final String SAVE_LANGUAGE = "save_language";


     private MultiLanguageUtil(){}
      public static MultiLanguageUtil getInstance(){
         return SingleInnerHolder.instance;
      }
      private static class SingleInnerHolder{
         private static MultiLanguageUtil instance = new MultiLanguageUtil();
      }

    /**
     * 设置语言
     */
    public void setConfiguration(ComponentActivity activity) {
        Locale targetLocale = getLanguageLocale();
        Configuration configuration = ApplicationUtils.context().getResources().getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(targetLocale);
        } else {
            configuration.locale = targetLocale;
        }
        Resources resources = ApplicationUtils.context().getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);//语言更换生效的代码!

        Intent intent =
                activity.getPackageManager().getLaunchIntentForPackage(activity.getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        activity.startActivity(intent);
        System.exit(0);


    }

    /**
     * 如果不是英文、简体中文、繁体中文，默认返回简体中文
     * 获取本地Locale类
     *
     * @return
     */
    public Locale getLanguageLocale() {
        int languageType = CommSharedUtil.getInstance(ApplicationUtils.context()).getInt(MultiLanguageUtil.SAVE_LANGUAGE, 0);
        if (languageType == LanguageType.LANGUAGE_FOLLOW_SYSTEM) {
            Locale sysLocale = getSysLocale();
            return sysLocale;
        } else if (languageType == LanguageType.LANGUAGE_EN) {
            return Locale.ENGLISH;
        } else if (languageType == LanguageType.LANGUAGE_CHINESE_SIMPLIFIED) {
            return Locale.SIMPLIFIED_CHINESE;
        } else if (languageType == LanguageType.LANGUAGE_CHINESE_TRADITIONAL) {
            return Locale.TRADITIONAL_CHINESE;
        } else if (languageType == LanguageType.LANGUAGE_JAPANESS) {
            return Locale.JAPAN;
        } else if (languageType == LanguageType.LANGUAGE_FRENCH) {
            return Locale.FRENCH;
        } else if(languageType == LanguageType.LANGUAGE_ARABIC){
            String languageToLoad  = "ar"; // your language
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            return locale;
        }
        getSystemLanguage(getSysLocale());
        Logs.INSTANCE.info("getLanguageLocale" + languageType + languageType);
        return Locale.SIMPLIFIED_CHINESE;
    }



    /**
     * 获取当前语言
     *
     * @return
     */
    public int getLanguageType() {
        int languageType = CommSharedUtil.getInstance(ApplicationUtils.context()).getInt(MultiLanguageUtil.SAVE_LANGUAGE, LanguageType.LANGUAGE_FOLLOW_SYSTEM);
        if (languageType == LanguageType.LANGUAGE_CHINESE_SIMPLIFIED) {
            return LanguageType.LANGUAGE_CHINESE_SIMPLIFIED;
        } else if (languageType == LanguageType.LANGUAGE_CHINESE_TRADITIONAL) {
            return LanguageType.LANGUAGE_CHINESE_TRADITIONAL;
        } else if (languageType == LanguageType.LANGUAGE_FOLLOW_SYSTEM) {
            return LanguageType.LANGUAGE_FOLLOW_SYSTEM;
        } else if (languageType == LanguageType.LANGUAGE_JAPANESS) {
            return LanguageType.LANGUAGE_JAPANESS;
        }
        Logs.INSTANCE.info("getLanguageType" + languageType);
        return languageType;
    }

    private String getSystemLanguage(Locale locale) {
        return locale.getLanguage() + "_" + locale.getCountry();

    }

    //以上获取方式需要特殊处理一下
    public Locale getSysLocale() {
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            locale = LocaleList.getDefault().get(0);
        } else {
            locale = Locale.getDefault();
        }
        return locale;
    }

    /**
     * 更新语言
     *
     * @param languageType
     */
    public void updateLanguage(int languageType,ComponentActivity activity) {
        CommSharedUtil.getInstance(ApplicationUtils.context()).putInt(MultiLanguageUtil.SAVE_LANGUAGE, languageType);
        MultiLanguageUtil.getInstance().setConfiguration(activity);
    }


    public static Context attachBaseContext(Context context) {
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return createConfigurationResources(context);
//        }
//        else {
//            MultiLanguageUtil.getInstance().setConfiguration();
//            return context;
//        }
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static Context createConfigurationResources(Context context) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        Locale locale = getInstance().getLanguageLocale();
        configuration.setLocale(locale);
        return context.createConfigurationContext(configuration);
    }
}
